package com.wind.base;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.wind.base.dialog.LoadingDialogHelper;
import com.wind.base.utils.SystemUiUtil;
import com.wind.toastlib.ToastUtil;
import com.wind.umengsharelib.UmengActivity;
import com.wind.view.TitleBar;


/**
 * Created by wind on 2017/11/28.
 */

public abstract class BaseActivity extends UmengActivity {

    protected TitleBar mTitleBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUiUtil.setStatusBarColor(this, Color.WHITE,false);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        View titleBar=$(R.id.title_bar);
        mTitleBar= (TitleBar) titleBar;
        if (mTitleBar!=null) {
            mTitleBar.setLeftIcon(R.drawable.icon_titlebar_back);
            mTitleBar.setTextColor(Color.parseColor("#333333"));
            mTitleBar.setLineColor(getResources().getColor(R.color.colordbdbdb));
            //mTitleBar.setLineVisibility(View.GONE);
        }
        setTitle();
    }


    public <T extends View> T $(int resId){
        return (T)super.findViewById(resId);
    }



    public boolean isFinish() {
        return this == null || isFinishing();
    }

    public Activity getActivity() {
        return this;
    }

    protected void setTitle() {
    }

    public void replaceFragment(Fragment fragment){
        if (fragment!=null){
            FragmentManager fm=getSupportFragmentManager();
            FragmentTransaction transaction=fm.beginTransaction();

            transaction.replace(R.id.fl_fragment_container,fragment);
            transaction.commitAllowingStateLoss();
        }

    }


    public String getText(TextView tv){
        return tv.getText().toString().trim();
    }

    public void showError(String msg) {
        ToastUtil.showToast(getActivity(),msg);
        hideOpLoadingIndicator();
    }


    public void showOpLoadingIndicator() {
        LoadingDialogHelper.showOpLoading(getActivity());
    }


    public void hideOpLoadingIndicator() {
        LoadingDialogHelper.hideOpLoading();
    }


}
